package class202204._21_25;

import java.util.Arrays;

class demo20220421 {
    /**
     * 查找数组中的元素
     * @param array
     * @param key
     * @return
     */
    public static int search(int[] array,int key) {
        for (int i = 0; i < array.length; i++) {
            if (array[i] == key){
                return i;
            }
        }
        //如果代码走到这里，说明退出循环
        return -1;
        //因为数组没有负数下标
    }
    public static void main1(String[] args) {
        int [] array = {1,2,3,4,5};
        int kno = search(array,4);
                if(kno == -1){
                    System.out.println("没有你要找的关键字");
                }else {
                    System.out.println("你要找的关键字下标为：" + kno);
                }
    }
    /**
     * 二分查找  前提是  数据必须是有序的
     * 定义 left  right
     */
    public static int binarysearach(int[] array,int key) {
        int lift = 0;
        int right = array.length-1;
        while (lift <= right){
            int mid = (lift+right)/2;
            if (array[mid]<key) {   //用array[mid]与key比较
                lift = mid + 1;
            }else if (array[mid]==key){
                    return mid;
            }else {
                    right = mid - 1;
            }
        }
        //走到这里  left>right
        return -1;//负数即可，数组中没有负数下标
    }

    public static void main2(String[] args) {
        int[] array ={1,2,5,9,12,43,54};
        int com = binarysearach(array,5);
        if (com==-1){
            System.out.println("没有你要找的关键字");
        }else {
            System.out.println("你要找的关键字下标为：" + com);
        }
    }
    /**
     * 如果没有顺序，先编序   但是不建议
     */
    public static int binarysearach1(int[] array,int key) {
        int lift = 0;
        int right = array.length-1;
        while (lift <= right){
            int mid = (lift+right)/2;
            if (array[mid]<key) {   //用array[mid]与key比较
                lift = mid + 1;
            }else if (array[mid]==key){
                return mid;
            }else {
                right = mid - 1;
            }
        }
        //走到这里  left>right
        return -1;//负数即可，数组中没有负数下标
    }

    public static void main3(String[] args) {
        int[] array ={1,2,5,9,12,7,54};
        Arrays.sort(array);//帮我们进行数组排序
        System.out.println("排序之后的数组：" + Arrays.toString(array));
        int com = binarysearach1(array,7);
        if (com==-1){
            System.out.println("没有你要找的关键字");
        }else {
            System.out.println("你要找的关键字下标为：" + com);
        }
    }

    /**
     * 二分法     数组是偶数数组  查找
     * @param array
     * @param key
     * @return
     */
    public static int binarysearach3(int[] array,int key) {
        int lift = 0;
        int right = array.length-1;
        while (lift <= right){
            int mid = (lift+right)>>>1;//无符号右移  相当于除以2
            if (array[mid]<key) {   //用array[mid]与key比较
                lift = mid + 1;
            }else if (array[mid]==key){
                return mid;
            }else {
                right = mid - 1;
            }
        }
        //走到这里  left>right
        return -1;//负数即可，数组中没有负数下标
    }

    public static void main4(String[] args) {
        int[] array ={1,2,5,12,43,54};
        int com = binarysearach3(array,12);
        if (com==-1){
            System.out.println("没有你要找的关键字");
        }else {
            System.out.println("你要找的关键字下标为：" + com);
        }
    }

    /**
     * equals用法  相同true  不同false
     * @param args
     */
    public static void main5(String[] args) {
        int[] array1 = {1,3,5,7,9};
        int[] array2 = {1,3,5,7,6};
        boolean flg = Arrays.equals(array1,array2);
        System.out.println(flg);
    }

    /**
     * fill  填充数组
     * @param args
     */
    public static void main6(String[] args) {
        int[] flg = new int[10];
        System.out.println("填充之前的数组："+ Arrays.toString(flg));
        Arrays.fill(flg,1);
        System.out.println("填充之后的数组：" +Arrays.toString(flg));
    }
    /**
     * 部分填充  不过界  左闭右开
     */
    public static void main7(String[] args) {
        int[] flg = new int[10];
        System.out.println("填充之前的数组："+ Arrays.toString(flg));
        Arrays.fill(flg,0,4,6);
        System.out.println("填充之后的数组：" +Arrays.toString(flg));
    }

    /**
     * 冒泡排序    并且  J已经优化
     * @param array
     */
    public static void bubblesort(int[] array) {
        for (int i = 0; i < array.length-1; i++) {
            for (int j = 0; j < array.length-1-i; j++) {
                if (array[j]>array[j+1]){
                    int stll =array[j];
                    array[j]=array[j+1];
                    array[j+1]=stll;
                }
            }
        }
    }

    public static void main8(String[] args) {
        int[] array = {16,31,8,54,25};
        System.out.println("没排序之前的数组："+Arrays.toString(array));
        bubblesort(array);
        System.out.println("排序之后的数组："+Arrays.toString(array));
    }

    /**
     * 冒泡排序   i优化
     * @param array
     */
    public static void bubblesort1(int[] array) {
        for (int i = 0; i < array.length-1; i++) {
            boolean flg = false;
            for (int j = 0; j < array.length-1-i; j++) {
                if (array[j]>array[j+1]){
                    int stll =array[j];
                    array[j]=array[j+1];
                    array[j+1]=stll;
                    flg = true;
                }
            }
            if (flg == false){
                break;
            }
        }
    }

    public static void main9(String[] args) {
        int[] array1 = {16,31,8,54,25};
        System.out.println("没排序之前的数组："+Arrays.toString(array1));
        bubblesort(array1);
        System.out.println("排序之后的数组："+Arrays.toString(array1));
    }

    /**
     * 数组逆序
     * @param array
     */
    public static void reverse(int[] array) {
        int left = 0;
        int right = array.length-1;
        int tem = 0;
        while (left<right){
            tem = array[left];
            array[left]=array[right];
            array[right]=tem;
            left++;
            right--;
        }
    }

    public static void main10(String[] args) {
        int[] array = {13,6,9,16,22};
        System.out.println("没有逆序之前的数组："+ Arrays.toString(array));
        reverse(array);
        System.out.println("逆序之后的数组："+ Arrays.toString(array));
    }

}